package com.monitor.service.parser.impl;

import com.monitor.model.ServerStatus;
import com.monitor.service.parser.MetricParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 磁盘指标解析器
 * 负责解析磁盘使用情况
 */
public class DiskMetricParser implements MetricParser {
    private static final Logger logger = LoggerFactory.getLogger(DiskMetricParser.class);

    @Override
    public void parse(String commandOutput, ServerStatus status) {
        try {
            List<ServerStatus.DiskInfo> diskInfoList = new ArrayList<>();
            String[] lines = commandOutput.split("\\n");
            
            // 跳过标题行
            for (int i = 1; i < lines.length; i++) {
                String line = lines[i].trim();
                // 只处理以/开头或包含 /的行，即挂载点
                if (line.startsWith("/") || line.contains(" /")) {
                    String[] parts = line.trim().split("\\s+");
                    
                    // df -h 输出格式：文件系统 容量 已用 可用 已用% 挂载点
                    if (parts.length >= 6) {
                        ServerStatus.DiskInfo diskInfo = new ServerStatus.DiskInfo();
                        int mountPointIndex = Math.max(5, parts.length - 1);
                        diskInfo.setMountPoint(parts[mountPointIndex]);
                        diskInfo.setTotal(parts[1]);
                        diskInfo.setUsed(parts[2]);
                        diskInfo.setPercentage(parts[4]);
                        diskInfoList.add(diskInfo);
                    }
                }
            }
            
            status.setDiskInfoList(diskInfoList);
        } catch (Exception e) {
            logger.error("解析磁盘信息失败: {}", e.getMessage());
        }
    }
} 